home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.mactech.com 2010
/
ftp.mactech.com.tar
/
ftp.mactech.com
/
challenge
/
13.08
/
ChallengeEqEval.sit.hqx
/
Challenge, Equation Evaluator
/
Evaluate.h
< prev
next >
Wrap
Text File
|
1997-06-25
|
5KB
|
217 lines
/*----------------------------------------------------------------------
Type definitions and function prototype from the Programmer's Challenge
problem statement.
----------------------------------------------------------------------*/
typedef unsigned long ulong;
typedef struct Values {
float first; // first value in sequence
float delta; // difference between consecutive values
ulong howMany; // number of values
} Values;
typedef struct IntValues {
long first; // first value in sequence
long delta; // difference between consecutive values
ulong howMany; // number of values
} IntValues;
typedef struct Results {
float equationResult; // result of equation, given x,y,n
float x; // value of x that produced equationResult
float y; // value of y that produced equationResult
long n; // value of n that produced equationResult
} Results;
void Evaluate(
char *equation, // null-terminated equation to evaluate
const Values *xP, // input values for x
const Values *yP, // input values for y
const IntValues *nP, // input values for n
Results w[] // preallocated storage for equation values
);
/*----------------------------------------------------------------------
These are types and function prototypes used internally
----------------------------------------------------------------------*/
extern ulong gVariableFlags;
extern ulong gNumConstants; // Number of constants found/stored
extern float *gConstants; // Array of constant values from equation
extern ulong *gCodeBase; // Address of start of generated code
extern ulong *gNextInstruction; // Where next generated instruction will be stored
union Constant {
float f; // a float constant
long which; // which one of a group (kVariable, kAddOp, etc.)
};
typedef union Constant Constant;
struct TokenNode {
int token;
Constant value;
struct TokenNode *left;
struct TokenNode *right; // unused for unary operators, functions
};
typedef struct TokenNode TokenNode;
//
// Return the next token in the equation. Adjust the pointer to
// point just after the token.
//
enum {
tokFloat,
tokLeftParen,
tokRightParen,
tokAddOp,
tokMulOp,
tokPowerOp,
tokNegate,
tokAbs,
tokRootOp,
tokFactorial,
tokFunction, // ln, sin, tan, etc.
tokVariable, // x, y, r, t, etc.
tokAssign, // =
tokInvalid
};
// Indices for the various functions
enum {
kFuncPower,
kFuncAbs,
kFuncSquareRoot,
kFuncFactorial,
kFuncExp,
kFuncLogN,
kFuncLogTen,
kFuncSin,
kFuncCos,
kFuncTan,
kFuncSec,
kFuncCsc,
kFuncCot,
kFuncArcSin,
kFuncArcCos,
kFuncArcTan,
kFuncArcSec,
kFuncArcCsc,
kFuncArcCot,
kFuncSinh,
kFuncCosh,
kFuncTanh,
kFuncSech,
kFuncCsch,
kFuncCoth,
kFuncAtan2
};
// Values of "which" for misc. tokens
enum {
kFloat,
kInteger,
kLeftParen,
kRightParen,
kAdd,
kSubtract,
kMultiply,
kDivide,
kExponent,
// "variables"
kPi,
kE,
kR,
kTheta,
kX,
kY,
kZ,
kN,
kEquals,
kInvalidToken
};
// Constants for bits in gVariableFlags
enum {
kRMask = 0x0001, // r was used in the equation
kThetaMask = 0x0002, // t was used in the equation
kXMask = 0x0004, // x was used in the equation
kYMask = 0x0008, // y was used in the equation
kNMask = 0x0010, // n was used in the equation
kEMask = 0x0020, // e was used in the equation
kPiMask = 0x0040, // p was used in the equation
kFunctionOfY= 0x0080 // function was of the form "z="
};
// The various integer and floating point registers used
enum {
regR0 = 0, // used for function calls
regTOC = 2, // TOC is pointer to globals
regFunctionGlue = 12,
regVariableBase = 13, // points to (temporary) values stored in memory
regFunctionBase = 14, // points to array of function pointers
regSavedLR = 15, // we save caller's LR here
regXValues = 16,
regYValues = 17,
regNValues = 18,
regNIntValues = 19,
regResults = 20,
regNInteger = 21,
regXCount = 22,
regYCount = 23,
regNCount = 24,
fpResult = 1, // results always in fp1
fpArg1 = 1, // first fp argument in fp1
fpArg2 = 2, // second fp argument in fp2
fpTemp1 = 3,
fpTemp2 = 4,
fpTempResult= 5,
regIntTemp1 = 4, // a volatile integer register
regFreeBase = 14, // first non-volatile register
regMax = 32, // number of fp registers
regN = 25,
regX = 26,
regY = 27,
regR = 28,
regTheta = 29,
regE = 30,
regPi = 31,
regMemory = 100 // fake register number for memory locations
};
// Token recognition routines
int GetToken(char **s, Constant *value);
int GetNumberToken(char **s, Constant *value);
// Token memory management
TokenNode *NewTokenNode(TokenNode *left, TokenNode *right, int token);
void FreeTree(TokenNode *root);
// Routines to parse various productions in the grammar
TokenNode *ParseExpression(char **s);
TokenNode *ParseTerm(char **s);
TokenNode *ParseFactor(char **s);
TokenNode *ParseNegative(char **s);
TokenNode *ParseFactorial(char **s);
TokenNode *ParseSimpleValue(char **s);
TokenNode *ParseStatement(char **s);
// Routines for creating and calling generated PowerPC code
void InitCodeGen(ulong *codeStart);
int CodeGenTree(TokenNode *root, int resultRegister);
void CodeGen(TokenNode *root);
float CallGeneratedCode(void *addr, const Values *xValues, const Values *yValues,
const IntValues *nIntValues, const Values *nValues,
Results *resultsPtr,
float *constants, void *functions,
float e, float pi);